import paramiko
import subprocess

class SFTP_CLIENT():

    def __init__(self, hostname, port=22, username="sdc", password="geMRint@adw2", ras_file=None):
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        self.hostname = hostname
        self.port =port
        self.username = username
        self.password = password
        self.ras_file = ras_file
        if ras_file is None:
            self.client.connect(self.hostname, self.port, username=self.username, password=self.password)
        else:
            self.client.connect(self.hostname, self.port, username=self.username, key_filename=self.ras_file)
        self.sftp_client = self.client.open_sftp()

    def exct_command(self, cmd):
        stdin, stdout, stderr = self.client.exec_command(cmd)
        print("stdout:", stdout.read().decode(encoding="utf8"))
        print("stderr:", stderr.read().decode(encoding="utf8"))

    def read_file(self,remote_file_path):
        f = self.sftp_client.file(remote_file_path)
        for line in f:
            print(line)
        f.close()

    def up_file(self,local_file_path, remote_file_path):
        self.sftp_client.put(local_file_path, remote_file_path)

        # if os.path.isfile(local_file_path):
        #     self.sftp_client.put(local_file_path, remote_file_path)
        # if os.path.isdir(local_file_path):
        #     all_up_files = []
        #     all_up_dirs = []
        #     for root, dirs, files in os.walk(local_file_path):
        #         # all_up_dirs.append(remote_file_path + "\\" + root)
        #         self.sftp_client.mkdir(os.path.join(remote_file_path, root))
        #         for file in files:
        #             # all_up_files.append(root + "\\" + file)
        #             self.sftp_client.put(root + "\\" + file, remote_file_path + "\\" + root + "\\" + file)

    def down_file(self,remote_file_path, local_file_path):
        self.sftp_client.get(remote_file_path, local_file_path)

    def client_close(self):
        self.client.close()
        self.sftp_client.close()



if __name__ == '__main__':
    # try:
    #     sftp = SFTP_CLIENT("10.189.130.234",username="sdc", password="adw2.0")
    #     sftp.exct_command("cd gfl_dir")
    #     print(1111111111111111)
    #     sftp.exct_command("tar -xf /home/sdc/gfl_dir/temp_desk.tar")
    #     sftp.client_close()
    # except Exception as e:
    #     print(e)
    # import subprocess
    # import os
    # import time
    # p1 = subprocess.Popen("cd temp_tar", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # print(p1.stdout.read(), p1.stderr.read())
    # p1.communicate()
    # os.chdir("temp_tar")
    # # p2 = subprocess.Popen("pwd", shell=True)
    # print(os.getcwd())

    sftp = SFTP_CLIENT("10.189.130.157", username="sdc")
    # sftp.exct_command(" && pwd && sudo setenv DISPLAY :0.0 && env  ")
    sftp.exct_command("  xterm -display :0.0 >/dev/null 2>&1  &;")
    sftp.exct_command("pwd")
    sftp.client.close()
